<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 回忆 <510974211@qq.com.com>
// +----------------------------------------------------------------------

if (!function_exists('spreadsheet_export')) {
	/**
	 * 数据导出
	 * @param string $xlsx_name    表格标题
	 * @param array  $ExportFields [['字段'=>'字段标题']]
	 * @param array  $ExportDatas  要导出的数量
	 * @return bool
	 */
	function spreadsheet_export($xlsx_name, $ExportFields, $ExportDatas)
	{
		empty($xlsx_name) && $xlsx_name = '数据导出';
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();

        $worksheet->setTitle('列表数据'); //设置表标题名称
        $worksheet->setCellValueByColumnAndRow(1, 1, $xlsx_name); //表头标题
 		
 		//设置单元格表头内容
        foreach (array_values($ExportFields) as $key => $value) {
            $worksheet->getColumnDimensionByColumn($key + 1)->setWidth(20);
            $worksheet->setCellValueByColumnAndRow($key + 1, 2, $value);
        }

        //合并单元格
        $worksheet->mergeCells('A1:E1');

        // 创建 A~AZ 的数组
        $AssignArr = [];
		for($i = 'A'; $i != 'BA'; $i++) {
			$AssignArr[] = $i;
		}

        $rows = 3;
        $fields = array_keys($ExportFields);
        foreach ($ExportDatas as $key => $value) {
            $worksheet->getColumnDimensionByColumn($rows)->setWidth(20);
            foreach ($fields as $fk => $field) {
                $worksheet->setCellValueByColumnAndRow($fk + 1, $rows, ($value[$field] ?? ''));
            }
            $rows ++;
        }

        $styleArray = [
            'font' => [
                'bold' => true
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        $CellEnd = $AssignArr[count($fields) - 1] ?? 'AZ'; //获取单元格最后一项位置

        //设置单元格样式
        $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
        $worksheet->getStyle("A2:{$CellEnd}2")->applyFromArray($styleArray)->getFont()->setSize(12);
        //添加所有边框/居中
        $worksheet->getStyle("A1:{$CellEnd}{$rows}")->applyFromArray([
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['argb' => '666666'],
                ],
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ]);

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $xlsx_name . '.xlsx"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

        $writer->save('php://output');
	}
}